package com.shupian.controller;

import com.shupian.domain.ResponseResult;
import com.shupian.domain.dto.LinkAddDto;
import com.shupian.domain.entity.Link;
import com.shupian.domain.vo.LinkByIdVo;
import com.shupian.enums.AppHttpCodeEnum;
import com.shupian.service.LinkService;
import com.shupian.utils.BeanCopyUtils;
import org.omg.CORBA.LongLongSeqHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Action;

@RestController
@RequestMapping("/content/link")
@Validated
public class LinkController {

    @Autowired
    private LinkService linkService;

    @GetMapping("/list")
    public ResponseResult getAllLinkPage(Integer pageNum, Integer pageSize, String name, String status){

        return linkService.getAllLinkPage(pageNum,pageSize,name,status);
    }

    @PostMapping
    public ResponseResult addLink(@RequestBody LinkAddDto linkAddDto){
        return linkService.addLink(linkAddDto);
    }

    @GetMapping("/{id}")
    public ResponseResult getLinkById(@PathVariable("id") Long id){
        Link link = linkService.getById(id);
        LinkByIdVo linkByIdVo = BeanCopyUtils.copyBean(link, LinkByIdVo.class);
        return ResponseResult.okResult(linkByIdVo);
    }

    @PutMapping
    public ResponseResult updateLink(@RequestBody LinkByIdVo link){
        Link link1 = linkService.getById(link.getId());
        link1.setAddress(link.getAddress());
        link1.setDescription(link.getDescription());
        link1.setName(link.getName());
        link1.setStatus(link.getStatus());
        link1.setLogo(link.getLogo());
        boolean b = linkService.updateById(link1);
        return b ? ResponseResult.okResult():ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteLink(@PathVariable("id") Long id){
        boolean b = linkService.removeById(id);
        return b ? ResponseResult.okResult():ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }







}
